<?php

namespace app\web\controller;

use think\Controller;
use think\Db;

class Base extends Controller {

    public $sysset;

    public function __construct(\think\App $app = null) {
        parent::__construct($app);

        $_GPC = input();
        $this->sysset = get_sysset();
        $this->view->assign('sysset', $this->sysset );
        $userinfo = dy_oauth_userinfo($this->sysset['basic']['client_key'], $this->sysset['basic']['client_secret']);
        if ($userinfo != 'success') {
            $msg = $userinfo;
            
            $this->view->assign('msg', $msg);
            return $this->view->fetch('public/message');
            exit;
        }
     
        $member = pdo_get('member', array('openid' => session('openid')));
        if (!$member) {
            $member = array('uid' => 0,
                'ismerch' => 0,
                'isagent' => 0,
                'agent_id' => 0,
                'agent_level' => 0,
                'openid' => session('openid'),
                'nickname' => session('fans')['nickname'],
                'realname' => '',
                'mobile' => '',
                'gender' => session('fans')['gender'],
                'avatar' => session('fans')['avatar'],
                'status' => 1,
                'createtime' => time(),
                'credit1' => 0,
                'credit2' => 0,
                'weixin' => '',
                'birthyear' => '',
                'birthmonth' => '',
                'birthday' => '',
                'province' => isset(session('fans')['province']) ? session('fans')['province'] : '',
                'city' => isset(session('fans')['city']) ? session('fans')['city'] : '',
                'area' => '',
                'num' => 0,
                'numing' => 0,
                'nums' => 0,
                'endtime' => 0,
                'access_token' => isset(session('fans')['access_token']) ? session('fans')['access_token'] : '');
            pdo_insert('member', $member);
            $member['id'] = Db::name('member')->getLastInsID();
        }
        if (isset(session('fans')['encrypt_mobile'])) {
            $mobile = openssl_decrypt(session('fans')['encrypt_mobile'], 'AES-256-CBC', $this->sysset['basic']['client_secret'], 0, substr($this->sysset['basic']['client_secret'], 0, 16));
                $member['mobile'] = $mobile;
                pdo_update('member', array('mobile' => $mobile), array('id' => $member['id']));
            
            unset(session('fans')['encrypt_mobile']);
            session('userinfo', base64_encode(serialize(session('fans'))));
        }
        if (isset(session('fans')['access_token']) && $member['access_token'] != session('fans')['access_token']) {
            $member['access_token'] = session('fans')['access_token'];
            pdo_update('member', array('access_token' => session('fans')['access_token']), array('id' => $member['id']));
        }
        session('fans', array_merge(session('fans'), $member));
        if (!isset($_COOKIE["formtoken"])) {
            setcookie('formtoken', md5(rand(1, 999)), time() + 7200, '/');
        }
        
          $_W=[
                'baseurl'=>$this->request->domain(true),
                'fans'=>session('fans')
            ];
            
            $this->view->assign('_W', $_W);
    }

}
